package com.hemaapp.yjnh.activity;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;

import com.hemaapp.yjnh.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VideoPlayActivity extends Activity {

    @Bind(R.id.video_view)
    VideoView videoView;
    @Bind(R.id.iv_video_play)
    ImageView ivVideoPlay;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.progressbar)
    ProgressBar progressbar;

    MediaController mController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        ButterKnife.bind(this);
        mController = new MediaController(this);
        String videoUrl = getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(videoUrl)) {
            Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT).show();
            finish();
        }else {
            init();
            if (videoUrl.startsWith("http"))
                videoView.setVideoURI(Uri.parse(videoUrl));
            else {
                videoView.setVideoPath(videoUrl);
            }
            videoView.start();
        }
    }
    @Override
    protected void onRestoreInstanceState(Bundle outState) {
        int sec = outState.getInt("time");
        videoView.seekTo(sec);
        super.onRestoreInstanceState(outState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        int sec = videoView.getCurrentPosition();
        outState.putInt("time", sec);
        super.onSaveInstanceState(outState);
    }

    private void init() {
        videoView.setMediaController(mController);
        videoView.requestFocus();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                progressbar.setVisibility(View.GONE);
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                ivVideoPlay.setVisibility(View.VISIBLE);
            }
        });
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                progressbar.setVisibility(View.GONE);
                return false;
            }
        });
    }

    @OnClick({R.id.iv_video_play, R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_video_play:
                ivVideoPlay.setVisibility(View.GONE);
                videoView.start();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
